<template>
    <div>
        <div style="display: inline-flex;margin-top: 20px">
            <el-input
                    v-model="name"
                    :disabled="disabled"
                    :placeholder="placeholder"
                    prefix-icon="el-icon-search"
                    style="width: 350px;margin-right: 10px"
                    clearable
                    @clear="search"
                    @keydown.enter.native="search"></el-input>
            <el-button
                    :disabled="disabled"
                    icon="el-icon-search"
                    type="primary"
                    @click="search">
                搜索
            </el-button>
        </div>
<!--        新增-->
        <el-button type="text" @click="dialogFormVisible = true">新增 Dialog</el-button>

        <el-dialog title="新增" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="名称" :label-width="formLabelWidth">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>


                <el-form-item label="头像" :label-width="formLabelWidth" style="text-align: left">
<!--                    <el-input v-model="form.image" autocomplete="off"></el-input>-->
                    <el-upload name="file"
                               :on-success="uploadImg"
                               action="http://localhost:8080/psychometricassessment/upload" list-type="picture-card" :auto-upload="true">
                        <i slot="default" class="el-icon-plus"></i>
                        <div slot="file" slot-scope="{file}">
                            <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
                            <span class="el-upload-list__item-actions">
                            <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                                 <i class="el-icon-zoom-in"></i>
                            </span>
                            <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                                 <i class="el-icon-download"></i>
                            </span>
                                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                                <i class="el-icon-delete"></i>
                                </span>
                            </span>
                        </div>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>



                <el-form-item label="数量" :label-width="formLabelWidth">
                    <el-input v-model="form.count" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="类型" :label-width="formLabelWidth">
                <el-input v-model="form.clazz" autocomplete="off"></el-input>
            </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="add">确 定</el-button>
            </div>
        </el-dialog>


<!--        修改-->
        <el-button type="text" @click="dialogFormVisible2 = true">修改 Dialog</el-button>

        <el-dialog title="修改" :visible.sync="dialogFormVisible2">
            <el-form :model="form">
                <el-form-item label="id" :label-width="formLabelWidth">
                    <el-input v-model="form.id" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="名称" :label-width="formLabelWidth">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
<!--                <el-form-item label="头像" :label-width="formLabelWidth">-->
<!--                    <el-input v-model="form.image" autocomplete="off"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="头像" :label-width="formLabelWidth">
                    <el-upload
                            action="http://localhost:8080/psychometricassessment/upload"
                            list-type="picture-card"
                            :on-success="uploadImg"
                            :on-preview="handlePictureCardPreview"
                            :on-remove="handleRemove">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    {{form.image}}
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>


                <el-form-item label="数量" :label-width="formLabelWidth">
                    <el-input v-model="form.count" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="分类" :label-width="formLabelWidth">
                    <el-input v-model="form.clazz" autocomplete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="update">确 定</el-button>
            </div>
        </el-dialog>


        <!--表单部分-->
        <el-table
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column
                    fixed
                    prop="id"
                    label="id"
                    width="150">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="名称"
                    width="220">
            </el-table-column>
            <el-table-column

                    label="头像"
                    width="320">
                <template slot-scope="scope">
                    <img :src="scope.row.image" style="width: 100px; height: auto;"/>
                </template>
            </el-table-column>
            <el-table-column
                    prop="count"
                    label="数量"
                    width="320">
            </el-table-column>
            <el-table-column
                    prop="clazz"
                    label="类型"
                    width="120">
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    width="100">
                <template slot-scope="scope">
                    <el-tooltip class="item" effect="dark" content="删除" placement="top">
                        <el-button type="danger" icon="el-icon-delete" circle @click="deleteVisible(scope.$index)"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>


    </div>
</template>

<script>
    export default {
        name: "PsychometricTesting",
        methods:{
            //模糊查询
            search(){
                this.axios.get("/psychometricassessment/findLike",
                    {
                        params:{
                            name:this.name
                        }
                    })
                    .then(resp=>{
                        console.log(resp)
                        const r = resp.data;
                        this.tableData = r.data;
                        // setTimeout(()=>{this.refresh()},500)
                    });
            },
            //上传相关的
            handleRemove(file) {
                console.log(file);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleDownload(file) {
                console.log(file);
            },


            uploadImg(resp) {
                console.log(resp.data);
                this.form.image = resp.data
            },
            //
            // uploadImg(r){
            //     console.log(r.data);
            //     this.tableData.picture="http://localhost:8080/psychometricassessment/down?filename="+r.data;
            //     console.log(this.tableData.picture)
            // },


            handleClick(row){
                console.log(row);
            },
            add(){
                this.axios.post("/psychometricassessment/add",{
                    name:this.form.name,
                    image:this.form.image,
                    count:this.form.count,
                    clazz:this.form.clazz
                }).then(resp=>{
                    const r=resp.data;
                    console.log(r);
                    // this.$message.success("添加成功");
                })
                //关闭
                this.dialogFormVisible = false;
            },
            update(){
                this.axios.put("/psychometricassessment/update",null,{
                    params:{
                        id:this.form.id,
                        name:this.form.name,
                        image:this.form.image,
                        count:this.form.count,
                        clazz:this.form.clazz
                    }
                }).then(resp=>{
                    const r=resp.data;
                    console.log(r);
                    this.$message.success("修改成功");
                })
                //关闭
                this.dialogFormVisible2 = false;
            },
            //删除
            deleteVisible(index){
                this.form.id = this.tableData[index].id
                //提交请求
                this.axios.delete("/psychometricassessment/del",{
                    params:{
                        id: this.form.id
                    }
                }).then((resp)=>{
                    const r = resp.data;
                    console.log(r);
                    this.$message.success("删除成功!")
                    setTimeout(()=>{this.refresh()},500)
                })
                // location.reload();
            },

            //
            refresh(){
                this.axios.get("/psychometricassessment/findAll")
                    .then(resp=>{
                        const r = resp.data;
                        this.tableData = r.data;
                    })
            }
        },
        data() {
            return{
                //    图像
                dialogImageUrl: '',
                dialogVisible: false,
                disabled: false,


                dialogFormVisible2: false,
                dialogFormVisible: false,
                form:{
                    id:'',
                    name:'',
                    image:'',
                    count:'',
                    clazz:''
                },
                name:'',
                // consult:{
                //     Startdate: new Data(),
                // },
                tableData: [{
                    id:'',
                    name:'',
                    image:'',
                    count:'',
                    clazz:''
                }],
                formLabelWidth: '120px'
            }
        },
        created(){
            this.refresh()
        },

    }
</script>

